cmake_minimum_required(VERSION 3.16)
project(iot_dtu_c C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
# 设置编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pthread")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -DDEBUG -DDTU_MEMORY_DEBUG")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG")
# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# 包含头文件目录 - 使用绝对路径确保CLion能找到
include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/third_party/paho-mqtt-c/include)
# 查找OpenSSL库
find_package(OpenSSL REQUIRED)
# 设置Paho MQTT库路径
set(PAHO_MQTT_LIB_DIR ${CMAKE_SOURCE_DIR}/third_party/paho-mqtt-c/lib)
# 定义源文件
set(CORE_SOURCES
        src/core/dtu_memory.c
        src/core/dtu_log.c
)
set(CONFIG_SOURCES
        src/config/dtu_config.c
        src/config/dtu_ini.c
)
set(NETWORK_SOURCES
        src/network/dtu_network.c
)
set(WEB_SOURCES
        src/web/dtu_web.c
)
set(AT_COMMAND_SOURCES
        src/at_command/dtu_at_command.c
)
set(MODBUS_SOURCES
        src/modbus/dtu_modbus.c
        src/modbus/dtu_modbus_poll.c
)
set(DATA_COLLECT_SOURCES
        src/data_collect/dtu_data_collect.c
)
set(DATA_PUSH_SOURCES
        src/data_push/dtu_data_push.c
)
set(MQTT_SOURCES
        src/mqtt/dtu_mqtt_paho.c
)
# 合并所有源文件
set(ALL_SOURCES
        main.c
        ${CORE_SOURCES}
        ${CONFIG_SOURCES}
        ${NETWORK_SOURCES}
        ${WEB_SOURCES}
        ${AT_COMMAND_SOURCES}
        ${MODBUS_SOURCES}
        ${DATA_COLLECT_SOURCES}
        ${DATA_PUSH_SOURCES}
        ${MQTT_SOURCES}
)
# 创建主程序
add_executable(iot_dtu_c ${ALL_SOURCES})
# 设置头文件路径
target_include_directories(iot_dtu_c PRIVATE ${CMAKE_SOURCE_DIR}/include)
# 链接库
target_link_libraries(iot_dtu_c
        pthread
        ${PAHO_MQTT_LIB_DIR}/libpaho-mqtt3c.a
        ${OPENSSL_LIBRARIES}
)
# 设备型号编译选项
option(BUILD_ETHERNET_ONLY "Build Ethernet only version" OFF)
option(BUILD_WIFI_ETHERNET "Build WiFi+Ethernet version" OFF)
option(BUILD_4G_ETHERNET "Build 4G+Ethernet version" OFF)
if(BUILD_ETHERNET_ONLY)
    target_compile_definitions(iot_dtu_c PRIVATE DTU_BUILD_ETHERNET_ONLY)
elseif(BUILD_WIFI_ETHERNET)
    target_compile_definitions(iot_dtu_c PRIVATE DTU_BUILD_WIFI_ETHERNET)
elseif(BUILD_4G_ETHERNET)
    target_compile_definitions(iot_dtu_c PRIVATE DTU_BUILD_4G_ETHERNET)
endif()
# 创建测试程序（简化版本）
option(BUILD_TESTS "Build test programs" OFF)
if(BUILD_TESTS)
    # 内存测试
    add_executable(test_memory
            tests/test_memory.c
            ${CORE_SOURCES}
    )
    target_include_directories(test_memory PRIVATE ${CMAKE_SOURCE_DIR}/include)
    target_link_libraries(test_memory pthread)
    # Modbus基础测试
    add_executable(test_modbus_basic
            tests/test_modbus_basic.c
            ${CORE_SOURCES}
            ${MODBUS_SOURCES}
            ${DATA_COLLECT_SOURCES}
            ${DATA_PUSH_SOURCES}
            ${MQTT_SOURCES}
    )
    target_include_directories(test_modbus_basic PRIVATE ${CMAKE_SOURCE_DIR}/include)
    target_link_libraries(test_modbus_basic pthread)
endif()
# 安装规则
install(TARGETS iot_dtu_c DESTINATION bin)
install(DIRECTORY config/ DESTINATION etc/dtu)
install(DIRECTORY web_ui/ DESTINATION share/dtu/web_ui)